import { adminArticle } from '@/api/content'

export let adArticle = {
    namespaced: true,
    state: () => ({
        articles: [],
        editArticle: {
            id: '',
            title: "",
            summary: "",
            source: "",
            image: "",
            content: "",
            keywords: "",
        },
        currentTabs: "data",
        showEdit: false,
        loading: false,
        count: -1
    }),
    mutations: {
        async getArticles(state, page){
            if(state.loading)return;
            state.loading = true; //解决element bug
            let data = await adminArticle.getAll(page)
            state.loading = false;
            state.articles = data.articles
            state.count = data.count;
        },
        unshiftArticle(state, article){
            state.articles.unshift(article)
        },
        spliceArticle(state, index){
            state.articles.splice(index, 1)
        },
        changeArticle(state, article){
            state.articles[state.editID] = article
            state.showEdit = false
            state.currentTabs = 'data'
        },
        setLoading(state, flag){
            state.loading = flag
        },
        async setEditArticle(state, scope){
            state.loading = true
            state.editID = scope.index_
            console.log('row_index', state.editID)
            let data = await adminArticle.getArticle(scope.row.id)
            console.log('article', data)
            state.loading = false
            state.editArticle = data;
            state.showEdit = true
            state.currentTabs = "edit"
        },
        cancelEdit(state){
            state.showEdit = false;
            state.currentTabs = "data";
            state.editArticle = {
                id: '',
                title: "",
                summary: "",
                source: "",
                image: "",
                content: "",
                keywords: "",
            };
        },
    }
}